﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility.DBUtility.DatabaseOperate;
using Utility.DBUtility.ORM;
using Utility.DBUtility.ORM.EntityAttribute;
using System.Reflection;
using Utility.Data.DbClient;
using Utility.Extensions;

namespace Utility.DBUtility
{
    /// <summary>
    /// Oracle 操作基础类
    /// </summary>
    public abstract class DbHelperFactory<T> : Repository<T> where T: Contracts.EntityBase
    {
        /// <summary>
        /// DB操作类
        /// </summary>
        protected override DbHelperBase DbHelper { get; set; }
        /// <summary>
        /// 数据库类型
        /// </summary>
        protected string m_DbType;
        /// <summary>
        /// 
        /// </summary>
        public DbHelperFactory(string connectionStringName = "DefaultConnection")
        {
            m_DbType = Utility.DBUtility.DBStringManage.Instance.GetDbType(connectionStringName).ToUpper();
            switch (m_DbType)
            {
                case "ORACLE":
                    DbHelper = new DbHelperOracle(connectionStringName);
                    break;
                case "SQLSERVER":
                    DbHelper = new DbHelperSQL(connectionStringName);
                    break;
                case "MYSQL":
                    DbHelper = new DbHelperMySql(connectionStringName);
                    break;
                case "SQLITE":
                    DbHelper = new DbHelperSQLite(connectionStringName);
                    break;
            }
        }
    }
}
/******************************  执行事务使用方法  *********************************
 *  Add by Snail 2018.04.26 增加 ORM 事务机制
 * 
 *    //开启并获取事务ID
 *    string transactionId = m_IQUICK_USERS.BeginTransaction();
 *    //设置另外一个or多个实例的事务ID
 *    m_IQUICK_LOGIN_PLATORM.Transactionid = transactionId;
 *    
 *    //执行sql
 *    m_IQUICK_LOGIN_PLATORM.Update(new Models.Entity.QUICK_LOGIN_PLATORM());
 *    m_IQUICK_USERS.Update(new Models.Entity.QUICK_USERS());
 *    
 *    //提交 or 回滚事务
 *    m_IQUICK_USERS.Commit();
 *    //m_IQUICK_USERS.Rollback();
 *    
 *    在Services 层 和 Data 层 都可以使用事务.
 *    
 *    并且具有相同 TransactionId 的实例, 任意一个实例进行提交和回滚都可以
 *
 *          
 **/
